package com.heima.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.apis.article.IArticleClient;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.behavior.dtos.BehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.user.config.CacheService;
import com.heima.user.mapper.ApUserFanMapper;
import com.heima.user.mapper.ApUserFollowMapper;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserBehaviorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thread.WmThreadLocalUtil;

@Service
public class ApUserBehaviorServiceImpl implements ApUserBehaviorService {

    private final CacheService cacheService;

    public ApUserBehaviorServiceImpl(CacheService cacheService) {
        this.cacheService = cacheService;
    }



    @Autowired
    private IArticleClient iArticleClient;

    @Autowired
    private ApUserFollowMapper apUserFollowMapper;

    @Autowired
    private ApUserFanMapper apUserFanMapper;

    @Autowired
    private ApUserMapper apUserMapper;

    /**
     * 关注与取消关注
     * @param userRelationDto
     * @return
     */
    @Override
    public ResponseResult followAndUnfollow(UserRelationDto userRelationDto) {
        // 1.检查参数
        if (userRelationDto == null || userRelationDto.getArticleId() == null || userRelationDto.getAuthorId() == null || userRelationDto.getOperation() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2.1根据作者id查询作者信息
        ApAuthor apAuthor = iArticleClient.getApAuthor(userRelationDto.getArticleId());
        // 2.2根据当前用户id查询当前用户信息
        Integer apUserId = WmThreadLocalUtil.getUser().getId();
        if (apUserId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH,"当前为游客登录，不能关注");
        }
        // redis的key
        String key = apUserId + "-" + userRelationDto.getArticleId();
        // 判断redis的key是否存在
        Boolean exists = cacheService.exists(key);
        BehaviorDto behaviorDto;
        if (exists) {
            String redisKey = cacheService.get(key);
            behaviorDto = JSON.parseObject(redisKey,BehaviorDto.class);
        } else {
            behaviorDto = new BehaviorDto();
        }
        /*ApUser apUser = apUserMapper.selectById(apUserId);
        // 3.1设置用户关注属性
        ApUserFollow apUserFollow = new ApUserFollow();
        apUserFollow.setUserId(apUserId);
        apUserFollow.setFollowId(apAuthor.getId());
        apUserFollow.setFollowName(apAuthor.getName());
        apUserFollow.setLevel((short) 0);
        apUserFollow.setIsNotice((short) 1);
        apUserFollow.setCreateTime(new Date());
        // 3.2设置用户粉丝属性
        ApUserFan apUserFan = new ApUserFan();
        apUserFan.setUserId(apAuthor.getId());
        apUserFan.setFansId(apUserId);
        apUserFan.setFansName(apUser.getName());
        apUserFan.setLevel((short) 0);
        apUserFan.setCreateTime(new Date());
        apUserFan.setIsDisplay((short) 1);
        apUserFan.setIsShieldLetter((short) 1);
        apUserFan.setIsShieldComment((short) 1);*/
        // 3.判断是关注还是取消关注
        if (userRelationDto.getOperation() == 0) {
            /*// 3.1保存信息到ap_user_follow
            apUserFollowMapper.insert(apUserFollow);
            // 3.2保存信息到ap_user_fan
            apUserFanMapper.insert(apUserFan);*/
            // 关注
            behaviorDto.setIsfollow(true);
        } else if (userRelationDto.getOperation() == 1){
            /*// 3.3删除ap_user_follow中的记录信息
            apUserFollowMapper.delete(Wrappers.<ApUserFollow>lambdaQuery().eq(ApUserFollow::getFollowId,userRelationDto.getAuthorId()));
            // 3.4删除ap_user_fan中的记录信息
            apUserFanMapper.delete(Wrappers.<ApUserFan>lambdaQuery().eq(ApUserFan::getUserId,apUserId));*/
            // 取消关注
            behaviorDto.setIsfollow(false);
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"你传入的参数有误，请检查！");
        }
        // 将值重新存入当前key
        cacheService.set(key,JSON.toJSONString(behaviorDto));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
